<?php
namespace Admin\Api\Controllers;

use Admin\Models\log\XhlogWechat;

class LogController  extends ControllerBase
{

    /**
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20170830
     *
     * @SWG\Post(
     *     path="/api/log/weChat",
     *     tags={"Log"},
     *     consumes={"application/json"},
     *     summary="提交微信聊天记录",
     *     description="提交微信聊天记录",
     *     @SWG\Parameter(name="body", in="body", description="聊天内容", required=true,
     *         @SWG\Schema(
     *              required={"sendTime", "content", "type"},
     *              @SWG\Property(property="sendTime", description="发送时间(时间戳，精确到秒，10位数字)", type="integer"),
     *              @SWG\Property(property="content", description="内容", type="string"),
     *              @SWG\Property(property="type", description="类型(1=文字，2=图片，3=语音，4=视频)", type="integer", enum={1, 2, 3, 4}, default="1"),
     *              @SWG\Property(property="senderAccount", description="发送者微信号", type="string"),
     *              @SWG\Property(property="senderName", description="发送者昵称", type="string"),
     *              @SWG\Property(property="senderMark", description="发送者备注", type="string"),
     *              @SWG\Property(property="receiverAccount", description="接收者微信号", type="string"),
     *              @SWG\Property(property="receiverName", description="接收者昵称", type="string"),
     *              @SWG\Property(property="receiverMark", description="接收者备注", type="string"),
     *         )
     *     ),
     *     @SWG\Response(response="200", ref="#/responses/Success"),
     *     @SWG\Response(response="400", ref="#/responses/Error"),
     *     @SWG\Response(response="500", ref="#/responses/Error"),
     * )
     */
    public function weChatAction()
    {
        if (!$this->request->isPost()){
            return $this->sendErrorResponse(400, '请求方式不正确');
        }
        $request = $this->request->getJsonRawBody(true);
        if (!$request['sendTime'] || !$request['content'] || !$request['type']) {
            return $this->sendErrorResponse(400, '缺少必要参数');
        }
        try {
            $log = new XhlogWechat($request);
            if (!$log->save()) {
                throw new \Exception('保存失败|' . $log->getMessages()[0]);
            }
            return $this->sendSuccessResponse('保存成功');
        }
        catch (\Exception $e) {
            $error = explode('|', $e->getMessage());
            return $this->sendErrorResponse(500, $error[0], $error[1] ?: '');
        }
    }

}

